/*     */ package org.poebot.hal;
/*     */ 
/*     */ import java.io.BufferedReader;
/*     */ import java.io.File;
/*     */ import java.io.FileNotFoundException;
/*     */ import java.io.FileReader;
/*     */ import java.io.IOException;
/*     */ import java.util.Vector;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class BotTrainer extends Thread
/*     */ {
/*     */   public static final int PARSE_BY_LINE = 3523;
/*     */   public static final int PARSE_BT_SENTENCE = 4123;
///*     */   private static final String END_CHARS = ".!?";
///*     */   private static final String WORD_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
/*     */   private final Chatbot bot;
/*     */   private final File file;
/*     */   private final int parseType;
/* 206 */   private Vector<BotTrainerListener> listeners = new Vector<BotTrainerListener>();
/*     */ 
/* 208 */   private static Logger logger = Logger.getLogger(BotTrainer.class);
/*     */ 
/*     */   public BotTrainer(Chatbot bot, File file, int parseType)
/*     */   {
/*  15 */     this.bot = bot;
/*  16 */     this.file = file;
/*  17 */     if (!file.canRead())
/*  18 */       logger.error("cannot read from training file " + file.getAbsolutePath());
/*  19 */     this.parseType = parseType;
/*     */   }
/*     */ 
/*     */   public void run()
/*     */   {
/*  25 */     switch (this.parseType)
/*     */     {
/*     */     case 3523:
/*  29 */       parseByLine();
/*     */ 
/*  31 */       break;
/*     */     case 4123:
/*  34 */       parseBySentence();
/*     */     }
/*     */   }
/*     */ 
/*     */   private void parseByLine()
/*     */   {
/*  42 */     long size = this.file.length();
/*  43 */     long amountRead = 0L;
/*  44 */     long lastUpdate = System.currentTimeMillis();
/*  45 */     logger.debug("training file size is " + size);
/*  46 */     if (!this.file.exists())
/*     */     {
/*  48 */       logger.warn("training file did not exist");
/*  49 */       return;
/*     */     }
/*     */ 
/*  52 */     BufferedReader trainReader = null;
/*     */     try {
/*  54 */       trainReader = new BufferedReader(new FileReader(this.file));
/*     */     } catch (FileNotFoundException e) {
/*  56 */       logger.error(e);
/*     */     }
/*  58 */     String line = "";
/*  59 */     while ((!isInterrupted()) && (line != null))
/*     */     {
/*     */       try {
/*  62 */         line = trainReader.readLine();
/*     */       } catch (IOException e) {
/*  64 */         logger.error(e);
/*     */       }
/*  66 */       this.bot.trainOnSentence(line);
/*  67 */       if (line != null)
/*  68 */         amountRead += line.length();
/*  69 */       if (System.currentTimeMillis() - lastUpdate <= 1L)
/*     */         continue;
/*     */       int percent;
///*     */       int percent;
/*  72 */       if (size > 0L)
/*  73 */         percent = (int)(amountRead * 100L / size);
/*     */       else
/*  75 */         percent = 0;
/*  76 */       progressUpdate(percent);
/*  77 */       lastUpdate = System.currentTimeMillis();
/*     */     }
/*     */     try
/*     */     {
/*  81 */       trainReader.close();
/*     */     } catch (IOException e) {
/*  83 */       logger.error(e);
/*     */     }
/*  85 */     trainingComplete();
/*     */   }
/*     */ 
/*     */   private void parseBySentence()
/*     */   {
/*  90 */     long size = this.file.length();
/*  91 */     long amountRead = 0L;
/*  92 */     long lastUpdate = System.currentTimeMillis();
/*  93 */     logger.debug("training file size is " + size);
/*  94 */     if (!this.file.exists())
/*     */     {
/*  96 */       logger.warn("training file did not exist");
/*  97 */       return;
/*     */     }
/*     */ 
/* 100 */     BufferedReader reader = null;
/*     */     try
/*     */     {
/* 103 */       reader = new BufferedReader(new FileReader(this.file));
/*     */     } catch (FileNotFoundException e) {
/* 105 */       logger.error(e);
/*     */     }
/* 107 */     StringBuffer buffer = new StringBuffer();
/* 108 */     int ch = 0;
/*     */     try {
/* 110 */       ch = reader.read();
/*     */     } catch (IOException e1) {
/* 112 */       e1.printStackTrace();
/*     */     }
/*     */     try
/*     */     {
/*     */       do {
/* 117 */         buffer.append((char)ch);
/* 118 */         if (".!?".indexOf((char)ch) >= 0) {
/* 119 */           int trailing_ch = 0;
/* 120 */           while (!isInterrupted())
/*     */           {
/* 122 */             trailing_ch = reader.read();
/* 123 */             if ((trailing_ch == -1) || ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".indexOf((char)trailing_ch) >= 0))
/*     */             {
/* 125 */               ch = trailing_ch;
/* 126 */               String sentence = buffer.toString();
/* 127 */               sentence = sentence.replace('\r', ' ');
/* 128 */               sentence = sentence.replace('\n', ' ');
/* 129 */               this.bot.trainOnSentence(sentence);
/* 130 */               logger.debug("Trained on the following: " + sentence);
/* 131 */               amountRead += sentence.length();
/*     */ 
/* 133 */               buffer = new StringBuffer();
/* 134 */               buffer.append((char)ch);
/*     */ 
/* 136 */               break;
/*     */             }
/*     */ 
/* 139 */             buffer.append((char)trailing_ch);
/*     */           }
/*     */         }
/*     */ 
/* 143 */         if (System.currentTimeMillis() - lastUpdate > 1000L)
/*     */         {
/*     */           int percent;
///*     */           int percent;
/* 146 */           if (size > 0L)
/* 147 */             percent = (int)(amountRead * 100L / size);
/*     */           else
/* 149 */             percent = 0;
/* 150 */           progressUpdate(percent);
/* 151 */           lastUpdate = System.currentTimeMillis();
/*     */         }
/*     */ 
/* 154 */         ch = reader.read();
/*     */ 
/* 116 */         if (isInterrupted()) break; 
/* 116 */       }while (ch != -1);
/*     */     }
/*     */     catch (IOException e)
/*     */     {
/* 159 */       logger.error(e);
/*     */       try
/*     */       {
/* 163 */         reader.close();
/*     */       } catch (IOException e1) {
/* 165 */         logger.error(e1);
/*     */       }
/*     */     }
/*     */     finally
/*     */     {
/*     */       try
/*     */       {
/* 163 */         reader.close();
/*     */       } catch (IOException e) {
/* 165 */         logger.error(e);
/*     */       }
/*     */     }
/* 168 */     trainingComplete();
/*     */   }
/*     */ 
/*     */   public void addListener(BotTrainerListener listener)
/*     */   {
/* 173 */     this.listeners.add(listener);
/*     */   }
/*     */ 
/*     */   public void removeListener(BotTrainerListener listener)
/*     */   {
/* 178 */     this.listeners.remove(listener);
/*     */   }
/*     */ 
/*     */   private void progressUpdate(int percent)
/*     */   {
/* 183 */     for (BotTrainerListener listener : this.listeners)
/* 184 */       listener.progressUpdate(percent);
/*     */   }
/*     */ 
/*     */   private void trainingComplete()
/*     */   {
/* 189 */     for (BotTrainerListener listener : this.listeners)
/* 190 */       listener.trainingComplete();
/*     */   }
/*     */ }

/* Location:           C:\workspace\dognuts\dognuts\dognuts.jar
 * Qualified Name:     org.dognuts.hal.BotTrainer
 * JD-Core Version:    0.6.0
 */